package com.cyq.day18.book;

public class BookManager {
    Book[] books = new Book[50];

    public BookManager() {
        books[0] = new Book(1, "星辰变", "我吃西红柿", 35, 77);
        books[1] = new Book(2, "校花的贴身高手", "鱼人二代", 44, 30);
        books[2] = new Book(3, "九鼎记", "我吃西红柿", 50, 70);
    }


    public void add(Book book) {
        boolean flag = false;
        for (int i = 0; i < books.length; i++) {
            if (books[i] != null) {
                if (books[i].getBookNum() == book.getBookNum()) {
                    System.out.println("此书重复！");
                    flag = true;
                    break;
                }
            }
        }
        if (!flag) {
            for (int i = 0; i < books.length; i++) {
                if (books[i] == null) {
                    books[i] = book;
                    break;
                }
            }
        }
    }

    public void list() {
        System.out.println("编号\t名称\t作者\t价格\t火热度");
        for (int i = 0; i < books.length; i++) {
            if(books[i] != null){
                System.out.println(books[i].getBookNum() + "\t"
                        + books[i].getBookName() + "\t"
                        + books[i].getAuthor() + "\t"
                        + books[i].getPrice() + "\t"
                        + books[i].getHotNum() + "\t");
            }
        }
    }


    public Book[] BookFilter(BookFilter bookFilter) {
        Book[] bb = new Book[50];
        int count = 0;
        for (int i = 0; i <books.length ; i++) {
            if (books[i]!=null){
                if (bookFilter.filter(books[i])){
                    bb[count]=new Book();
                    bb[count].setBookNum(books[i].getBookNum());
                    bb[count].setAuthor(books[i].getAuthor());
                    bb[count].setBookName(books[i].getBookName());
                    bb[count].setPrice(books[i].getPrice());
                    bb[count].setHotNum(books[i].getHotNum());
                    count++;

                }
            }
        }
        return bb;
    }

    public Book[] getBooks() {
        return books;
    }

    public void setBooks(Book[] books) {
        this.books = books;
    }
}
